#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>


#define BEEP_CNT     1           /* 设备号个数 */
#define BEEP_NAME    "beep"      /* 名字 */
#define BEEPOFF          0           /* 关灯 */
#define BEEPON           1           /* 开灯 */



struct beep_dev
{
    dev_t devid;    /*  设备号 */
    struct cdev cdev; /* cdev */
    struct class *class; /* 类 */
    struct device *device; /* 设备 */
    int major;             /* 主设备号 */
    int minor;              /* 次设备号 */
    struct device_node *nd; /* 设备节点 */
    int beep_gpio; /* 所使用的gpio编号 */
};

struct beep_dev beep;    /* beep设备 */


static int beep_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &beep;
    return 0;
}

static ssize_t beep_read(struct file *filp, const char __user *buf,size_t cnt,loff_t *offt)
{
    return 0;
}


static ssize_t beep_write(struct file *filp, const char __user *buf,size_t cnt,loff_t *offt)
{
    int retval;
    unsigned char databuf[1];
    unsigned char beepstate;

    struct beep_dev *dev = filp->private_data;

    retval = copy_from_user(databuf,buf,cnt);

    beepstate = databuf[0];

    if(beepstate == BEEPOFF){
        gpio_set_value(dev->beep_gpio,0);
    }else if(beepstate == BEEPON){
        gpio_set_value(dev->beep_gpio,1);
    }
    return retval;
}

static int beep_release(struct inode *inode, struct file *fillp)
{
    return 0;
}

/* 设备操作函数 */
static struct file_operations beep_fops = {
    .owner	= THIS_MODULE,
    .open = beep_open,
    .read = beep_read,
    .write = beep_write,
    .release = beep_release
};

static int __init beep_init(void)
{
    int ret = 0;

    /* 设置beep所使用的beep */
    /* 1、获取设备节点 beep*/
    beep.nd = of_find_node_by_path("/beep");
    if (beep.nd != NULL)
    {
        printk("beep node has been found!\r\n");
    }

    /* 2、beep 的GPIO */
    beep.beep_gpio = of_get_named_gpio(beep.nd,"beep-gpio",0);
    ret = gpio_direction_output(beep.beep_gpio,1);


    /* 注册字符设备驱动 */
    /* 1、创建设备号 */
    if(beep.major){
        beep.devid = MKDEV(beep.major,0);
        register_chrdev_region(beep.devid,BEEP_CNT,BEEP_NAME);
    }else{
        alloc_chrdev_region(&beep.devid,0,BEEP_CNT,BEEP_NAME);   /* 申请设备号 */
        beep.major = MAJOR(beep.devid);    /* 获取分配的主设备号 */
        beep.minor = MINOR(beep.devid);   /*  获取分配的次设备号 */
    }

    printk("beep major = %d, minor = %d \r\n",beep.major,beep.minor);


    /*  2、初始化 */
    beep.cdev.owner = THIS_MODULE;
    cdev_init(&beep.cdev,&beep_fops);

    /*  3、添加一个cdev */
    cdev_add(&beep.cdev,beep.devid,BEEP_CNT); 

    /* 创建类 */
    beep.class = class_create(THIS_MODULE,BEEP_NAME);
    if(IS_ERR(beep.class)){
        return PTR_ERR(beep.class);
    }

    /*  5、创建设备 */
    beep.device = device_create(beep.class,NULL,beep.devid,NULL,BEEP_NAME);

    if(IS_ERR(beep.device)) {
        return  PTR_ERR(beep.device);
    }

    return 0;
}

static void __exit beep_exit(void)
{
    /*  注销字符设备驱动 */
    cdev_del(&beep.cdev);
    unregister_chrdev_region(beep.devid,BEEP_CNT);

    device_destroy(beep.class,beep.devid);
    class_destroy(beep.class);
}



module_init(beep_init);
module_exit(beep_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chaoliang");
